<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="elibrary.DaoConnection" %>
<%@ page import="elibrary.DesEncrypter"%>
<%@ page language="java" import="java.util.*"%>
<%@ include file="config.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>

        <%
        try
        {
                //the user id is fetched and the if the user is not valid then he is redirected to the index page
	
                String uid=(String)session.getAttribute("uid");
                if(uid==null||uid.equals(""))
                {
                        response.sendRedirect("index.jsp");
                }
	
                //decryption of the user id is done
	
                DesEncrypter encrypter = new DesEncrypter();
                int uid1 = Integer.parseInt(encrypter.decrypt(uid));
	
                //the reservation id is fetched from the previous page and that reservation is canceled
	
	
                int resvno = Integer.parseInt(request.getParameter("resvid"));
                Connection con=DaoConnection.getcon(connectURL, DBuser, DBpass);
                Statement st=con.createStatement();
                PreparedStatement ps=null;
                String query = "DELETE FROM RESERVATION WHERE RESVNO = "+resvno;
                ps=con.prepareStatement(query);
                int res = ps.executeUpdate();
	
                if (res == 1) {
                        session.setAttribute("message", "Reservation cancelled succesfully.");
                        response.sendRedirect("showReservation.jsp");
		
                }
                else {
                        session.setAttribute("message", "You tried to delete an invalid reservation.");
                        response.sendRedirect("showReservation.jsp");
                }
        }catch(Exception e){
             e.printStackTrace();
        }
        %>

    </body>
</html>